// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include <stddef.h>

extern int memcmp(const void *s1, const void *s2, size_t n) __attribute__((weak));
extern const char *strchr(const char *s, char c) __attribute__((weak));
extern void *memchr(const void *s, int c, size_t n) __attribute__((weak));

// TODO: Faster implementation.
int
memcmp(const void *s1, const void *s2, size_t n)
{
	const unsigned char *m1 = s1;
	const unsigned char *m2 = s2;

	for (size_t i = 0; i < n; i++) {
		if (m1[i] != m2[i]) {
			return m1[i] - m2[i];
		}
	}

	return 0;
}

const char *
strchr(const char *s, char c) {
	while (*s != c && *s != 0)
		s++;
	if (*s == 0)
		return NULL;
	return s;
}

void *
memchr(const void *s, int c, size_t n)
{
	const unsigned char *ptr = s;
	while (n > 0) {
		if (*ptr == (unsigned char)c) {
			return (void *)ptr;
		}
		ptr++;
		n--;
	}
	return NULL;
}

// TODO: memcpy, memmove, memset, strcmp
